package com.bondex.charter.mq;

import com.bondex.charter.service.edi.EdiRawService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 调用记录相关MQ监听
 */
@Component
public class MQReceive {

	//region 参数

	//监听EDI报文变更
	@Value("${mq.listener.edi_raw.exchange}")
	private String EDI_RAW_EXCHANGE;
	@Value("${mq.listener.edi_raw.routing_key}")
	private String EDI_RAW_ROUTING_KEY;

	@Autowired
	private EdiRawService ediRawService;

	//endregion

	@RabbitListener(queues = "${mq.listener.queue}", concurrency = "${mq.listener.concurrency}")
	@RabbitHandler
	public void mqReceive(Message oMessage) {
		try {
			String sMsg = new String(oMessage.getBody(), "UTF-8");

			String sExchange = oMessage.getMessageProperties().getReceivedExchange();
			String sRoutingKey = oMessage.getMessageProperties().getReceivedRoutingKey();

			if (EDI_RAW_EXCHANGE.equals(sExchange)
					&& EDI_RAW_ROUTING_KEY.equals(sRoutingKey)) {
				//监听EDI报文变更
				ediRawService.handlerEdiRaw(sMsg);
			} else {
				//不知道改怎么处理的时候
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
